// pages/form/index.js
import {__get,__upload, __post, __put} from '../../service/request.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      begin_at: '',
      end_at: '',
      company: '',
      team: '',
      description: '',
      law: '',
      system: '',
      grade: '',
      peccancy: '',
      audit: '',
      proposal: '',
      inspector: '',
      unit: '',
      point: '',
      rectify: '',
      problem: '',
      rectification: ''
    },
    images: {
      inspector: '',
      unit: '',
      point: '',
      rectify: '',
      problem: '',
      rectification: ''
    },
    inputID: '',
    inputValue: '',
    inputName: '',
    showInput: false,
    showSign: false,
    showSelectdMore: false,
    selectdData: {
      law: [],
      system: [],
      grade: [],
      peccancy: [],
      audit: [],
      proposal: []
    },
    selectdTitle: '',
    getSelectdList: false,
    cate: 0,
    title: '',
    checkedValue: '',
    actionType: 'add',
    orderID: ''
  },
  startX: 0,
  startY: 0,
  context: {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        actionType: 'edit',
        orderID: options.id
      })
      this.getOrderInfo(options.id)
    }
  },
  // 提交
  btnSumbit() {
    __post('lists', this.data.form, res => {
      wx.navigateBack({
        delta: 1
      })
    })
  },
  // 获取表单内容
  getOrderInfo(id) {
    const __this = this;
    const app = getApp();
    let images = this.data.images;
    __get('lists/' +id , {}, res => {
      for (const key in images) {
        images[key] = app.data.imageUrl + res[key];
      }
      console.log(images)
      __this.setData({
        form: res,
        images: images
      })
    })
  },
  // 修改
  btnEdit() {
    __put('lists/' + this.data.orderID, this.data.form, res => {
      wx.navigateBack({
        delta: 1
      })
    })
  },
  // date changed
  dateChange(e) {
    let form = this.data.form;
    form[e.target.id] = e.detail.value;
    this.setData({form: form});
  },
  // show input
  showInput(e) {
    this.setData({
      inputID: e.target.id,
      inputValue: this.data.form[e.target.id],
      showInput: true
    })
  },
  // input change
  inputChange(e) {
    this.setData({
      inputValue: e.detail.value
    });
  },
  inputBtnSumbit() {
    let form = this.data.form;
    form[this.data.inputID] = this.data.inputValue;
    this.setData({
      form: form,
      showInput: false
    });
  },
  inputBtnChen() {
    this.setData({
      showInput: false
    })
  },
  // input end
  // 3级选择
  showSelectMore(e) {
    this.setData({
      showSelectdMore: true,
      inputName: e.target.dataset.name,
      inputID: e.target.dataset.id,
      selectdTitle: '',
      getSelectdList: true
    })
    this.getSelectData();
  },
  getSelectData () {
    let data = this.data.selectdData;
    if (data[this.data.inputID].length == 0) {
      this.toSeach();
    } else {
      this.setData({
        getSelectdList: false
      })
    }
  },
  // sousuo
  seachTextChange(e) {
    this.setData({
      selectdTitle: e.detail.value
    })
  },
  toSeach() {
    const __this = this;
    let data = this.data.selectdData;
    let params = {
      type: __this.data.inputID,
      title: this.data.selectdTitle
    }
    wx.showLoading({
      title: '正在获取数据...',
    });
    __get('wx-categories', params, res => {
      data[__this.data.inputID] = res;
      __this.setData({
        selectdData: data,
        getSelectdList: false
      })
      wx.hideLoading();
    });
  },
  // cate click
  cateClick(e) {
    this.setData({
      cate: e.currentTarget.id
    })
  },
  titleClick(e) {
    this.setData({
      title: e.currentTarget.id
    })
  },
  checkChange(e) {
    const value = e.detail.value;
    let v = '';
    for(const key in value) {
      v += value[key]
    }
    this.setData({
      checkedValue: v
    })
  },
  checkedClick() {
    let form = this.data.form;
    form[this.data.inputID] = this.data.checkedValue;
    this.setData({
      form: form,
      checkedValue: ''
    })
    this.checkedChen();
  },
  checkedChen() {
    this.setData({
      showSelectdMore: false
    })
  },
  // select end
  // upload image
  uploadImg(e) {
    const imageType = e.target.id;
    const __this = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const path = res.tempFilePaths[0];
        let images = __this.data.images;
        let form = __this.data.form;
        __upload('iamges', path, res => {
          images[imageType] = path;
          form[imageType] = res;
          __this.setData({
            form: form,
            images: images
          })
          console.log(__this.data.images)
        })
      },
    })
  },

  // 签名
  signClick(e) {
    this.setData({
      showSign: true,
      inputID: e.target.id
    });
    console.log(e.target.id)
  },
  touchStart(e) {
    this.startX = e.changedTouches[0].x;
    this.startY = e.changedTouches[0].y;
    this.context = wx.createContext();

    this.context.setStrokeStyle('#000000');
    this.context.setLineWidth(5);
    this.context.setLineCap('round');
    this.context.beginPath();
  },
  touchMove(e) {
    let startX = e.changedTouches[0].x;
    let startY = e.changedTouches[0].y;

    this.context.moveTo(this.startX, this.startY);
    this.context.lineTo(startX, startY);
    this.context.stroke();

    this.startX = startX;
    this.startY = startY;

    wx.drawCanvas({
      canvasId: 'signCanvas',
      actions: this.context.getActions(),
      reserve: true
    })
  },
  signBtnSumbit () {
    const __this = this;
    let form = this.data.form;
    let images = this.data.images;
    wx.canvasToTempFilePath({
      canvasId: 'signCanvas',
      destWidth: 150,
      destHeight: 50,
      success: res => {
        const image = res.tempFilePath;
        __upload('signTrue', image, res => {
          form[__this.data.inputID] = res;
          images[__this.data.inputID] = image;
          __this.setData({
            form: form,
            images: images
          });
          __this.signChen();
        })
      }
    })
  },
  signChen() {
    const ctx = wx.createCanvasContext('signCanvas');
    ctx.clearRect(0,0, 1000, 200);
    ctx.draw();
    this.setData({
      showSign: false
    })
  },
  // 签名结束

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})